<!DOCTYPE html>
<html>
<head>
    <% include ../include/css_header.html %>
    <style>
    </style>
</head>
<body class="actlist_bg">
<div class="container">
    <div class="row">
        <div style="position: fixed;top: 10px;z-index: 10;" class="col-xs-12">
            <a class="icon-arrow-left" style="color: #ffffff;font-size: 24px;" href="javascript:window.history.go(-1);"></a>
        </div>
    </div>
    <div style="height: 20px;"></div>
    <div class="font_shadow_133000" style="text-align: center;color: #ffffff;">
        <h3><strong><%=order.goodsList[0].goodsName %></strong></h3>
    </div>
    <div class="row" style="min-height: 460px">
        <div class="col-xs-12">
            <div class="bg_op_panel" style="padding: 15px">
                <div class="bg_white_panel">
                    <img src="/goods/<%=factionId %>_<%=order.goodsList[0].goodsId %>.jpg" class="" style="width: 285px;"/>
                    <hr>
                    <ul>
                        <li><strong><%=order.goodsList[0].goodsName %></strong></li>
                        <li>订单总额: <%=order.totalFee %> <span style="float: right">积分余额: <%=factionMember.score%></span></li>
                        <li>成交时间: <%=dateUtil.formatDateNew(order.creatTime, 'yyyy-MM-dd hh:mm') %></li>
                        <li>状态: <%if(order.status==='waiting'){%>未支付<%}%>
                            <%if(order.status==='pay'){%>已支付<%}%>
                            <%if(order.status==='finish'){%>已完成<%}%>
                        </li>
                    </ul>
                    <hr/>
                    <div id="addressDiv">
                        <% if(order.address){ %>
                        <ul style="font-size: 12px;">
                            <li>收货人: <%=order.address.reciver %></li>
                            <li>收货地址: <%=order.address.city %><%=order.address.detail %></li>
                            <li>邮编: <%=order.address.postal %></li>
                            <li>联系方式: <%=order.address.contact %></li>
                        </ul>
                        <% }else{ %>

                        <% } %>
                    </div>
                    <%if(order.status==='waiting'){%>
                    <button type="button" class="btn btn-lg btn-primary btn-block" id="selectAddrBtn">选择收货地址</button>
                    <button type="button" class="btn btn-lg btn-warning btn-block" id="payBtn">马上兑换</button>
                    <button type="button" class="btn btn-lg btn-default btn-block" id="cancelBtn">取消订单</button>
                    <%}%>
                </div>
            </div>
        </div>
    </div>
    <% include ../include/copyright.html %>
</div>
<% include ../include/js_footer.html %>
<script>
    var authCode = '<%=authCode %>';
    var address = {};
    $(function(){
        $('#selectAddrBtn').on('click', function(){
            getAddress();
        });
        $('#payBtn').on('click', function(){
            payOrder();
        });
        $('#cancelBtn').click(function(){
            cancelOrder();
        })
    });
    function cancelOrder(){
        $.post('/cancelOrder', {id: '<%=order._id %>'}, function(res){
            window.location.href = '/myOrder/waiting'
        })
    }
    function payOrder(){
        $.post('/payCurrentOrder', {
            id: '<%=order._id%>',
            factionId: '<%=factionId %>',
            address: address
        }, function(res){
            if(res.error){
                return AlertBox.showTip('余额不足～');
            }else{
                AlertBox.showTip('支付完成！');
                setTimeout(function(){
                    window.location.href = '/myOrder/pay'
                }, 500)
            }
        })
    }
    function getAddress(){
        $.post('/getWechatAddress', {
            code: authCode
        }, function(res){
            if(res.error) return alert('....');
            var params = res.params;
            WeixinJSBridge.invoke('editAddress',{
                "appId": params.appId,
                "scope": "jsapi_address",
                "signType": "sha1",
                "addrSign": params.addrSign,
                "timeStamp": params.timeStamp,
                "nonceStr": params.nonceStr
            }, function(res){
                address = {
                    reciver: res.userName,
                    city: res.addressCitySecondStageName+res.addressCountiesThirdStageName,
                    detail: res.addressDetailInfo,
                    postal: res.addressPostalCode,
                    contact: res.telNumber
                }
                var html = '<ul style="font-size: 12px;">\
                                <li>收货人: '+res.userName+'</li>\
                                <li>收货地址: '+res.addressCitySecondStageName+res.addressCountiesThirdStageName+res.addressDetailInfo+'</li>\
                                <li>邮编: '+res.addressPostalCode+'</li>\
                                <li>联系方式: '+res.telNumber+'</li>\
                            </ul>'
                $('#addressDiv').html(html)
            })
        })

    }
</script>
</body>
</html>